//
//  ProductOrderItemCell.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/5/8.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "ProductOrderItemCell.h"

#import "GoodsDetailsViewController.h"

@interface ProductOrderItemCell()<UIGestureRecognizerDelegate>

@property (nonatomic,strong) UIView *bgView;

@property (nonatomic,strong) UIImageView *logoImgV;

@property (nonatomic,strong) UILabel *nameLB;

@property (nonatomic,strong) UIButton *toDetailBtn;

@property (nonatomic,strong) UILabel *orderStatusLB;//订单状态lb

@property (nonatomic,strong) UIView *goodsBgView;

@property (nonatomic,strong) UIImageView *goodsImgV;//商品图片

@property (nonatomic,strong) UILabel *goodsInfoLB;//商品介绍lb

@property (nonatomic,strong) UILabel *goodsSkuLB;//商品型号lb

@property (nonatomic,strong) UILabel *goodsPriceLB;//商品价格lb

@property (nonatomic,strong) UILabel *goodsNumberLB;//商品数量lb

@property (nonatomic,strong) UIButton *evaluationBtn;//评价

@property (nonatomic,strong) UIButton *statusBtn;//订单状态

@property (nonatomic,strong) UIView *orderInfoView;
@property (nonatomic,strong) UILabel *orderNumberLB;//订单数量lb
@property (nonatomic,strong) UILabel *orderPriceLB;//订单价格lb
@property (nonatomic,strong) UIButton *orderBtn1;
@property (nonatomic,strong) UIButton *orderBtn2;
@property (nonatomic,strong) UIButton *orderBtn3;

@end

@implementation ProductOrderItemCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}
#pragma mark - setter
- (void)setModel:(ProductOrderModel *)model{
    _model = model;
    
    self.nameLB.text = _model.company;
    self.nameLB.width = [Configuration getLabelWidthWithText:self.nameLB.text withFont:self.nameLB.font];
    self.toDetailBtn.left = self.nameLB.right + 5;
        
    self.orderPriceLB.text = [Configuration getTotalPriceWithPrice:_model.payprice withTbPrice:_model.totaltbprice];
    self.orderNumberLB.text = [NSString stringWithFormat:@"共%ld件",_model.number];
    self.orderStatusLB.text = _model.status;
    
    [self.goodsImgV sd_setImageWithURL:[NSURL URLWithString:_model.picture]];
    self.goodsInfoLB.text = _model.name;
    self.goodsSkuLB.text = _model.norms;
    self.goodsSkuLB.width = [Configuration getLabelWidthWithText:self.goodsSkuLB.text withFont:self.goodsSkuLB.font];
    self.goodsPriceLB.text = [NSString stringWithFormat:@"%ldTB",(long)_model.tbprice];
    self.goodsNumberLB.text = [NSString stringWithFormat:@"X%ld",(long)_model.number];
    
    self.evaluationBtn.hidden = YES;
    [self.statusBtn setTitle:_model.status forState:normal];
    self.statusBtn.right = self.evaluationBtn.right;
    
    self.orderBtn1.hidden = NO;
    [self.orderBtn1 setTitle:@"查看详情" forState:normal];
    NSMutableArray *btnTitlesArr = [NSMutableArray array];
    if (_model.donebtn == YES) {
        [btnTitlesArr addObject:@"确认完成"];
    }
    if (_model.commentbtn == YES) {
        [btnTitlesArr addObject:@"去评价"];
    }
    if (_model.refundbtn == YES) {
        [btnTitlesArr addObject:@"申请退款"];
    }
    if (_model.rejectbtn == YES) {
        [btnTitlesArr addObject:@"申请退换货"];
    }
    if (_model.trackbtn == YES) {
        [btnTitlesArr addObject:@"查看物流"];
    }
    if (btnTitlesArr.count >= 1) {
        self.orderBtn2.hidden = NO;
        [self.orderBtn2 setTitle:btnTitlesArr[0] forState:normal];
    }
    if (btnTitlesArr.count >= 2) {
        self.orderBtn2.hidden = NO;
        self.orderBtn3.hidden = NO;
        [self.orderBtn2 setTitle:btnTitlesArr[0] forState:normal];
        [self.orderBtn3 setTitle:btnTitlesArr[1] forState:normal];
    }
}

#pragma mark - UI
-(void)initUI{
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.logoImgV];
    [self.bgView addSubview:self.nameLB];
    [self.bgView addSubview:self.toDetailBtn];
    [self.bgView addSubview:self.orderStatusLB];
    
    [self.bgView addSubview:self.goodsBgView];
    [self.goodsBgView addSubview:self.goodsImgV];
    [self.goodsBgView addSubview:self.goodsInfoLB];
    [self.goodsBgView addSubview:self.goodsSkuLB];
    [self.goodsBgView addSubview:self.goodsPriceLB];
    [self.goodsBgView addSubview:self.goodsNumberLB];
    
    [self.bgView addSubview:self.orderInfoView];
    
    [self.orderInfoView addSubview:self.orderNumberLB];
    [self.orderInfoView addSubview:self.orderPriceLB];
    [self.orderInfoView addSubview:self.orderBtn1];
    [self.orderInfoView addSubview:self.orderBtn2];
    [self.orderInfoView addSubview:self.orderBtn3];
    
}
#pragma mark - lazy

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 12, ScreenWidth, 110+160)];
        _bgView.clipsToBounds = YES;
        _bgView.layer.cornerRadius = 8;
        _bgView.backgroundColor = UIColor.whiteColor;
    }
    return _bgView;
}
- (UIImageView *)logoImgV{
    if (!_logoImgV) {
        _logoImgV = [[UIImageView alloc] initWithFrame:CGRectMake(16, 17, 15, 15)];
        _logoImgV.image = LoadImage(@"商品详情_店铺");
    }
    return _logoImgV;
}

- (UILabel *)nameLB{
    if (!_nameLB) {
        _nameLB = [[UILabel alloc] initWithFrame:CGRectMake(self.logoImgV.right + 4, self.logoImgV.top - 2, 80, 20)];
        _nameLB.textColor = MainBlackTextColor;
        _nameLB.font = FontSet(15);
        _nameLB.textAlignment = NSTextAlignmentLeft;
        
        _nameLB.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toStoreClick:)];
        tap.delegate = self;
        [_nameLB addGestureRecognizer:tap];
    }
    return _nameLB;
}
- (UIButton *)toDetailBtn{
    if (!_toDetailBtn) {
        _toDetailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _toDetailBtn.frame = CGRectMake(self.nameLB.right + 5, self.nameLB.top, 40, 20);
        [_toDetailBtn setImage:LoadImage(@"个人中心_查看全部") forState:normal];
        [_toDetailBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 34)];
        [_toDetailBtn addTarget:self action:@selector(toDetailClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _toDetailBtn;
}

- (UILabel *)orderStatusLB{
    if (!_orderStatusLB) {
        _orderStatusLB = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 95, self.toDetailBtn.top - 1, 80, 18)];
        _orderStatusLB.textColor = RGB(255, 34, 21);
        _orderStatusLB.font = FontSet(15);
        _orderStatusLB.textAlignment = NSTextAlignmentRight;
    }
    return _orderStatusLB;
}


- (UIView *)goodsBgView{
    if (!_goodsBgView) {
        _goodsBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, ScreenWidth, 110)];
        _goodsBgView.clipsToBounds = YES;
        _goodsBgView.layer.cornerRadius = 8;
        _goodsBgView.backgroundColor = UIColor.whiteColor;
    }
    return _goodsBgView;
}
//商品图片
-(UIImageView *)goodsImgV{
    if (!_goodsImgV) {
        _goodsImgV = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 80, 80)];
        _goodsImgV.backgroundColor = MainBackColor;
        _goodsImgV.clipsToBounds = YES;
        _goodsImgV.layer.cornerRadius = 4;
        
        _goodsImgV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        tap.delegate = self;
        [_goodsImgV addGestureRecognizer:tap];
    }
    return _goodsImgV;
}
//商品信息简介lb
-(UILabel *)goodsInfoLB{
    if (!_goodsInfoLB) {
        _goodsInfoLB = [[UILabel alloc] initWithFrame:CGRectMake(self.goodsImgV.right+10, self.goodsImgV.top, ScreenWidth - 140, 20)];
        _goodsInfoLB.numberOfLines = 1;
        _goodsInfoLB.textAlignment = NSTextAlignmentLeft;
        _goodsInfoLB.textColor = MainBlackTextColor;
        _goodsInfoLB.font = FontSet(14);
    }
    return _goodsInfoLB;
}
//商品信息简介lb
-(UILabel *)goodsSkuLB{
    if (!_goodsSkuLB) {
        _goodsSkuLB = [[UILabel alloc] initWithFrame:CGRectMake(self.goodsImgV.right+10, self.goodsInfoLB.bottom+8, 50, 17)];
        _goodsSkuLB.textAlignment = NSTextAlignmentLeft;
        _goodsSkuLB.backgroundColor = MainBackColor;
        _goodsSkuLB.textColor = MainGrayTextColor;
        _goodsSkuLB.font = FontSet(12);
        _goodsSkuLB.clipsToBounds = YES;
        _goodsSkuLB.layer.cornerRadius = 2;
    }
    return _goodsSkuLB;
}
//商品价格lb
-(UILabel *)goodsPriceLB{
    if (!_goodsPriceLB) {
        _goodsPriceLB = [[UILabel alloc] initWithFrame:CGRectMake(self.goodsImgV.right+10, self.goodsImgV.bottom - 18, 80, 18)];
        _goodsPriceLB.textAlignment = NSTextAlignmentLeft;
        _goodsPriceLB.textColor = RGB(255, 34, 21);
        _goodsPriceLB.font = FontSetWithWeight(15, 0.2f);
    }
    return _goodsPriceLB;
}
//商品数量简介lb
-(UILabel *)goodsNumberLB{
    if (!_goodsNumberLB) {
        _goodsNumberLB = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-75, self.goodsImgV.bottom-20, 35, 20)];
        _goodsNumberLB.textAlignment = NSTextAlignmentRight;
        _goodsNumberLB.textColor = MainGrayTextColor;
        _goodsNumberLB.font = FontSet(14);
    }
    return _goodsNumberLB;
}
- (UIButton *)evaluationBtn{
    if (!_evaluationBtn) {
        _evaluationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _evaluationBtn.frame = CGRectMake(self.goodsNumberLB.left - 55, self.goodsNumberLB.top-1, 52, 22);
        _evaluationBtn.clipsToBounds = YES;
        _evaluationBtn.layer.cornerRadius = 11;
        _evaluationBtn.layer.borderWidth = 1;
        _evaluationBtn.layer.borderColor = RGBA(255, 193, 77, 0.5f).CGColor;
        [_evaluationBtn setTitle:@"去评价" forState:normal];
        [_evaluationBtn setTitleColor:RGB(255, 188, 62) forState:normal];
        [_evaluationBtn.titleLabel setFont:FontSet(12)];
        [_evaluationBtn addTarget:self action:@selector(evaluationClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _evaluationBtn;
}
- (UIButton *)statusBtn{
    if (!_statusBtn) {
        _statusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _statusBtn.frame = CGRectMake(self.evaluationBtn.left - 65, self.goodsNumberLB.top-1, 52, 22);
        [_statusBtn setTitleColor:RGB(255, 188, 62) forState:normal];
        [_statusBtn.titleLabel setFont:FontSet(12)];
    }
    return _evaluationBtn;
}
- (UIView *)orderInfoView{
    if (!_orderInfoView) {
        _orderInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, self.goodsBgView.bottom, ScreenWidth, 95)];
        _orderInfoView.backgroundColor = UIColor.whiteColor;
    }
    return _orderInfoView;
}
- (UILabel *)orderPriceLB{
    if (!_orderPriceLB) {
        _orderPriceLB = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 150, 12, 135, 18)];
        _orderPriceLB.font = FontSet(13);
        _orderPriceLB.textColor = MainBlackTextColor;
        _orderPriceLB.textAlignment = NSTextAlignmentLeft;
    }
    return _orderPriceLB;
}
- (UILabel *)orderNumberLB{
    if (!_orderNumberLB) {
        _orderNumberLB = [[UILabel alloc] initWithFrame:CGRectMake(self.orderPriceLB.left - 120, 12, 110, 18)];
        _orderNumberLB.font = FontSet(13);
        _orderNumberLB.textColor = MainGrayTextColor;
        _orderNumberLB.textAlignment = NSTextAlignmentRight;
    }
    return _orderNumberLB;
}

- (UIButton *)orderBtn1{
    if (!_orderBtn1) {
        _orderBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        _orderBtn1.frame = CGRectMake(ScreenWidth - 100, self.orderPriceLB.bottom+24, 84, 30);
        _orderBtn1.clipsToBounds = YES;
        _orderBtn1.layer.cornerRadius = 15;
        [_orderBtn1 setTitleColor:RGB(255, 34, 21) forState:normal];
        _orderBtn1.layer.borderWidth = 1;
        _orderBtn1.layer.borderColor = RGBA(255, 34, 21, 0.5f).CGColor;
        [_orderBtn1.titleLabel setFont:FontSet(14)];
        [_orderBtn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _orderBtn1.hidden = YES;
    }
    return _orderBtn1;
}
- (UIButton *)orderBtn2{
    if (!_orderBtn2) {
        _orderBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        _orderBtn2.frame = CGRectMake(self.orderBtn1.left - 100, self.orderBtn1.top, 84, 30);
        _orderBtn2.clipsToBounds = YES;
        _orderBtn2.layer.cornerRadius = 15;
        [_orderBtn2 setTitleColor:MainPureColor(119) forState:normal];
        _orderBtn2.layer.borderWidth = 1;
        _orderBtn2.layer.borderColor = MainPureColor(220).CGColor;
        [_orderBtn2.titleLabel setFont:FontSet(14)];
        [_orderBtn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _orderBtn2.hidden = YES;
    }
    return _orderBtn2;
}
- (UIButton *)orderBtn3{
    if (!_orderBtn3) {
        _orderBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
        _orderBtn3.frame = CGRectMake(self.orderBtn2.left - 100, self.orderBtn1.top, 84, 30);
        _orderBtn3.clipsToBounds = YES;
        _orderBtn3.layer.cornerRadius = 15;
        [_orderBtn3 setTitleColor:MainPureColor(119) forState:normal];
        _orderBtn3.layer.borderWidth = 1;
        _orderBtn3.layer.borderColor = MainPureColor(220).CGColor;
        [_orderBtn3.titleLabel setFont:FontSet(14)];
        [_orderBtn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _orderBtn3.hidden = YES;
    }
    return _orderBtn3;
}
#pragma mark - SEL
//去评价
-(void)evaluationClick{
}
//进入店铺
-(void)toStoreClick:(UIGestureRecognizer *)ges{
    [self toDetailClick];
}
//进入店铺
-(void)toDetailClick{
    
}
//底部按钮点击事件
-(void)btnClick:(UIButton *)btn{
    if (self.touchBlock) {
        self.touchBlock(self.model, btn.titleLabel.text);
    }
}

//点击头像进入商品详情
-(void)tapClick:(UIGestureRecognizer *)ges{
    [self jumpGoodsDetailWithID:self.model.productId];
}
-(void)jumpGoodsDetailWithID:(NSInteger)goodsId{
    GoodsDetailsViewController *vc = [[GoodsDetailsViewController alloc] init];
    vc.goodsID = goodsId;
    [[Configuration getCurrentVC].navigationController pushViewController:vc animated:YES];
}


@end
